﻿using ITXHWEB.Models;
using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Web.Mvc;
using WPFExcelReport;
using WPFExcelReport.Helpers;
using ITXHWEB.Infrastructure;
using System.Collections;
using System.Collections.Generic;
namespace ITXHWEB.Controllers
{
    public partial class VVTLIEFController : ITXController
    { 
        [ChildActionOnly]
        public ActionResult SVVTLIEF(string returnUrl)
        {
            ModelState.Clear();
            ViewBag.ReturnUrl = returnUrl;
            if(MABF==null)
            {
                MABF = new ABFOLGEModel();
                MABF.CWBB = new WBBSTModel(); 
            }
            VVTLIEFModel m = new VVTLIEFModel();
            m.TERMNO = app.TERMNO;
            m.VKPAUF = VKPAUF;
            m.LIEFSCH = LIEFSCH;
            if (!IsVVTReady)
            {
                m.REMARK = VKPAUF;
            }
            return PartialView(m);
        }
        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult SVVTLIEF(VVTLIEFModel model, string returnUrl)
        {
            try
            {
                ModelState.Clear();
                LIEFSCH = model.LIEFSCH.Trim(); 
                VVTLOAD(model);
                MABF.CVVT.LIEFSCH = LIEFSCH;
                MABF.IsVVTReady = IsVVTReady;
                MABF.IsPDCReady = true;
            }
            catch (Exception ex)
            {
                VKPAUF = ex.Message;
                Tracer.Error(ex.StackTrace, ex);
            }
            return RedirectToAction(MAPP, MAPP);
            //return RedirectToAction("ITXWB06", "ITXWB06", new { TempData = TempData });
            //return View(model);
        } 
        public void VVTLOAD(VVTLIEFModel model)
        {
            WBBS = null;
            IsVVTReady = false;
            if (VKPAUF == null)
                return;
            if (VKPAUF.Trim().Length < 8)
            {
                IsVKPReady = false;
            }
            else
            {
                IsVKPReady = true;
            }
            if (LIEFSCH == null)
                return;
            if (LIEFSCH.Trim().Length < 5)
            {
                IsVVTReady = false;
                return;

            }
            VKPAUF = VKPAUF.Trim().PadRight(11);
            PM["AUFTYP"] = "K";
            PM["KRS"] = VKPAUF.Substring(0, 2);
            PM["LFD"] = VKPAUF.Substring(2, 6);
            PM["POS"] = VKPAUF.Substring(8, 3);
            PM["LIEFSCH"] = LIEFSCH.Trim().PadRight(8);

            DataSet SResult = VHP.VVT.VVTLOAD(PM); 
            foreach (DataRow row in SResult.Tables[0].Rows)
            {

                MABF.CVVT.LIEFSCH = row["VVTLIEFSCH"].ToString();
                MABF.CVVT.AUFTYP = row["VVTAUFTYP"].ToString();
                MABF.CVVT.KRS = row["VVTAUFKRS"].ToString();
                MABF.CVVT.LFD = row["VVTAUFLFD"].ToString();
                MABF.CVVT.POS = row["VVTAUFPOS"].ToString();
                MABF.CVVT.VKPAUF = row["VVTAUFKRS"].ToString() + row["VVTAUFLFD"].ToString() + row["VVTAUFPOS"].ToString();
                MABF.CVVT.VFART = row["VVTART"].ToString() + row["VVTFARBE"].ToString();
                MABF.CVVT.WAKZ = row["VVTWAKZ"].ToString();
                MABF.CVVT.ART = row["VVTART"].ToString();
                MABF.CVVT.VAR = row["VVTVAR"].ToString();
                MABF.CVVT.FARBE = row["VVTFARBE"].ToString();
                MABF.CVVT.CONTROL = row["CONTROL"].ToString();
                LCKQTY = double.Parse(row["VVTROHGEW"].ToString())*0.01;
                IsVVTReady = true;
            }
        }
    }
}
